<?php
session_start();
include('funct.php');

$sql="select DATE_FORMAT(wizyty.data,'%k:%i'), pac.pac_nazwisko, pac.pac_imie, wizyty.id, wizyty.id_pac, sl_uslugi.nazwa from sl_uslugi, pac, wizyty where wizyty.typ_wiz = sl_uslugi.id and wizyty.akt = 1 and pac.id = wizyty.id_pac and wizyty.data like '".date("Y-m-d")."%' and wizyty.id_gab = ".$_SESSION['wyb_gab']." order by wizyty.data";

$result=mysql_query($sql);
$ile_pac=mysql_numrows($result);

while($row = mysql_fetch_array($result))
	{
		$dat_wiz=strtotime($row[0]);
		$pac_na_dzis[$dat_wiz]=$row;
	}

echo '<table><tr class="linia"><td>Pacjenci na dziś</td><td>Przyszłe wizyty</td></tr>';
if ($pac_na_dzis<>''){
echo '<tr><td  valign="top"><table>';


foreach ($pac_na_dzis as $i=>$w){

$sql="select * from wizyty_inf where id_wiz=".$w[3];
$result=mysql_query($sql);
$czy_zapisany=mysql_num_rows($result);

#$wiz = @mysql_fetch_array($result);

	echo '<tr class="linia"><td>';
        
        $pac_stat_ubezp = check_pac_ubezp($w[4]);
    //print_r($pac_stat_ubezp);
    if ($pac_stat_ubezp[0] == '1'){ 
        echo '<img src="./img/uziel.png" title="'.$pac_stat_ubezp[1].'" width=16px>';
    }
    elseif ($pac_stat_ubezp[0] == '0'){
        echo '<img src="./img/uczerw.png" title="'.$pac_stat_ubezp[1].'" width=16px>';
    }
    else {
        echo '<img src="./img/upom.png" title="'.$pac_stat_ubezp[1].'" width=16px>';
    }
        
	if ($czy_zapisany==1) {
            echo '<a href="index.php?realiz_wiz='.$w[3].'&realiz_pac_id='.$w[4].'"><font color=red><b>'.$w[0].' '.$w[1].' '.$w[2].'-'.$w[5].'</b></font></a>';
            #echo '<a href="./wydruki/wydr_wiz.php?wiz='.$w[3].'" target="_blank"><img src="./img/print.png"></a><br>';
        }
	else echo '<a href="index.php?realiz_wiz='.$w[3].'&realiz_pac_id='.$w[4].'"><font color=green>'.$w[0].' '.$w[1].' '.$w[2].'-'.$w[5].'</font></a>';
	echo '</td></tr>';
}

echo '</table></td><td>';
}
else echo '<tr><td  valign="top"><table><tr><td><font color=red><b>Brak pacjentów zapisanych na dziś</b></td></tr></table></td><td>';
#if ($pac_na_dzis<>''){
echo '<table>';
#foreach ($pac_na_dzis as $i=>$w){
echo '<tr class="linia"><td>';

$jutro=date("Y-m-d", strtotime("+1 day"));
$za_tydz=date("Y-m-d", strtotime("+8 day"));
	
$sql="select sl_gab.nazwa, sl_gab.id from wizyty, pac, sl_gab, user_gab where wizyty.id_gab = sl_gab.id and sl_gab.id = user_gab.id_gab and user_gab.id_user = ".$_SESSION['uzytk']['id']." and  pac.id = wizyty.id_pac and  wizyty.data > '".$jutro."%' and  wizyty.data < '".$za_tydz."%' and wizyty.akt=1 group by sl_gab.id, sl_gab.nazwa order by sl_gab.id";
#echo $sql;
$result=mysql_query($sql);

$nr=1;
while($row = mysql_fetch_array($result))
	{
	if (fmod($nr, 2)==1) echo '<table bgcolor=#00ff00 width=100%>';	
	else echo '<table bgcolor=#11ccff width=100%>';	
	
	
	
	echo '<tr><td>'.$row[0].'</td>';
	
	$sql4="select pac.pac_nazwisko, pac.pac_imie, DATE_FORMAT(wizyty.data, '%Y-%m-%d %k:%i') as czas from wizyty, pac, sl_gab, user_gab where wizyty.id_gab = sl_gab.id and sl_gab.id = user_gab.id_gab and user_gab.id_user = ".$_SESSION['uzytk']['id']." and  pac.id = wizyty.id_pac and  wizyty.data > '".$jutro."%' and  wizyty.data < '".$za_tydz."%' and sl_gab.id = ".$row[1]." and wizyty.akt=1 order by sl_gab.id, wizyty.data ";
	$result4=mysql_query($sql4);
	echo  '<td>';
	while($row4 = mysql_fetch_array($result4))
	{
	
	echo $row4[2].' '.$row4[0].' '.$row4[1].'<br>';
	}
	echo '</td>';
	echo '</tr>';
	echo '</table>';
	$nr++;
}
echo '</td></tr>';
	
	
	echo '</table>';


echo '</td></tr></table>';

?>
